#!/usr/bin/python

### CODE BY KYLE CLEMENS ###
### COPYRIGHT KYLE CLEMENS 2011 ###
### DISTRIBUTED UNDER TERMS OF THE GNU GPL V3 ###

import os
import commands
from sys import argv
import getopt
uid = os.getuid()
cmdline_params = argv[1:]

verbose  = False
vverbose = False

try:
	opts, args = getopt.gnu_getopt(cmdline_params, 'hi', ['help', 'is-root', 'install-verbose', 'install-really-verbose'])
except getopt.GetoptError:
	print "Unexpected option. Quitting."
	quit()
else:
	pass
	
for option, parameter in opts:
	if option in ('-h', '--help'):
		print "dl Installer"
		print "-h, --help                Show this help"
		print "--is-root                 Bypass root verification"
		print '-i, --install-verbose     Install verbose mode executable as well as non-verbose'
		print '--install-really-verbose  Install really verbose mode. Call it with dlvv'
		print
		print "Just run install to install dl"
		quit()
	if option in ('--is-root'):
		uid = 0
	if option in ('-i', '--install-verbose'):
		verbose = True
		print "Installing verbose mode as well as normal. Call verbose mode with dlv rather than dl"
	if option in ('--install-really-verbose'):
		vverbose = True
		print "Installing really verbose mode with normal mode. Call really verbose mode with dlvv rather than dl"
	
if uid != 0:
	print "Must be root to install, if you are root, run with the --is-root argument."
	quit()

print "Moving dl to /usr/bin/"
os.system('cp src/dl /usr/bin/')
print "Setting permissions"
os.system('chmod +x /usr/bin/dl')
if verbose:
	print "Moving dlv to /usr/bin"
	os.system('cp src/dlv /usr/bin')
	print 'Setting permissions'
	os.system('chmod +x /usr/bin/dlv')
if vverbose:
	print "Moving dlvv to /usr/bin"
	os.system('cp src/dlvv /usr/bin')
	print 'Setting permissions'
	os.system('chmod +x /usr/bin/dlvv')
print "Installing manpage"
dir = commands.getoutput("man -w man")
dir = dir.partition("man1")
os.system("cp src/dl.7 %s/man7/dl.7" % dir[0])
print "Done!"
quit()